package com.jingdianjichi.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * DruidEncryptUtil 类
 *
 * @author 熊苏鹏1
 * @date 2025/1/25 13:42
 * @description 数据库加密
 */
public class DruidEncryptUtil {
private static String  publicKey;

private static String privateKey;
static{
    try {
        String[] strings = ConfigTools.genKeyPair(512);
        privateKey = strings[0];
        System.out.println("privateKey:"+privateKey);
        publicKey = strings[1];
        System.out.println("publicKey:"+publicKey);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchProviderException e) {
        throw new RuntimeException(e);
    }

}
public static String encrypt(String plainText) throws Exception {
    return ConfigTools.encrypt(privateKey, plainText);
}
public static String decrypt(String encryptText) throws Exception {
    return ConfigTools.decrypt(publicKey, encryptText);
}

    public static void main(String[] args) throws Exception {
    String encrypt = encrypt("jdbc:mysql://localhost:3306/jc-club?useUnicode=true&characterEncoding=utf8&useSSL=true");
        System.out.println("encrypt   "+encrypt);
        System.out.println("decrypt  "+decrypt(encrypt));
    }
    
}
